package cl.xintec.utils
{
	import flash.utils.getTimer;
	
	import mx.events.ModuleEvent;
	import mx.modules.IModuleInfo;
	import mx.modules.ModuleLoader;
	import mx.modules.ModuleManager;
	
	public class ModuleLoader10
	{
		
		private var _modules:Array = new Array();
		private var _ready:Array = new Array();
		
		public function ModuleLoader10()
		{
		}

		public function preloadModulesWithHandle(moduleName:String, s:String, handleReady:Function, handleError:Function):void {
		    // Get a reference to the module's interface.
		    var info:IModuleInfo = 
		        ModuleManager.getModule(s);
		    info.addEventListener(ModuleEvent.READY, handleReady);
		    info.addEventListener(ModuleEvent.ERROR, handleError);
		    
		    // Load the module into memory.
		    trace(getTimer() + ": " + "preloadModules : " + moduleName + "- " + s + "- Cargando en memoria");            
		    info.load();
		    _modules[s] = info;
		}

		public function preloadModules(moduleName:String, s:String):void {
		    // Get a reference to the module's interface.
		    var info:IModuleInfo = 
		        ModuleManager.getModule(s);
		    info.addEventListener(ModuleEvent.READY, modEventHandler);
		    info.addEventListener(ModuleEvent.ERROR, modErrorEventHandler);
		    
		    // Load the module into memory.
		    trace(getTimer() + ": " + "preloadModules : " + moduleName + "- " + s + "- Cargando en memoria");            
		    info.load();
		    _modules[s] = info;
		}
				
		private function modEventHandler(e:ModuleEvent):void {
			trace(getTimer() + ": " + "modEventHandler : " + e.module.url + " - " + e.type);
			_ready[e.module.url] = true; 
		}

		private function modErrorEventHandler(e:ModuleEvent):void {
			trace(getTimer() + ": " + "modErrorEventHandler : " + e.module.url + "- " + e.type + "- " + e.errorText);
			_ready[e.module.url] = false;
		}

		
		public function createModule(m:ModuleLoader, s:String):void {
        	m.url = s;
	    
	    	m.loadModule();
		}
		
		public function removeModule(m:ModuleLoader):void {
		    m.unloadModule();
		}

	}
}